home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
EnigmA Amiga Run 1997 May
/
EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso
/
softwareupdate
/
system
/
amigados
/
miscellaneous
/
miscellaneous.doc
< prev
next >
Wrap
Text File
|
1996-10-10
|
12KB
|
358 lines
8 MISCELLANEOUS
8.1 INTRODUCTION
In this chapter I will describe some very useful AmigaDOS
functions which did not fit in the other chapters. Although
some knowledge about AmigaDOS is required to understand this
chapter you do not have to read all previous chapters before
this one. The introduction chapter should be enough.
The following subjects will be described:
1. The Global Error Code
2. How to execute "Shell" commands from a C program
Not much for the moment, but more will soon be added!!!
8.2 THE GLOBAL ERROR CODE
When a dos function fails it will usually store some
information about the error, an error code, in a special global
variable. You can get a copy of this error code with the help
of the IoErr() function.
There are three good reasons why you should call the IoErr()
function when you encounter a dos error:
1. First you need to inform the user and it is then of course
good to know what really have happened so you can tell the
user to take appropriate action.
2. The more your program knows about the error the easier it
is for your program to solve it.
3. Some times you need to check if it really was an error or
not when a dos function failed. For example, in the
previous chapter you read about how to list all objects in
a directory. When no more objects can be found the
function, ExNext(), will fail. This is usually not any
"real" error since there are of course a limited number of
objects in a directory, and that dos function should
"fail" when all objects have been listed. It is in these
situations you have to check what the actual error was -
if there were not any more objects in the directory it is
OK, but if it was something else which caused the function
to fail there was a "real" error.
The IoErr() function is defined like this:
---------------------------------------------------------------
IoErr()
ROM library: "dos.library/IoErr", (Any Version)
#include <clib/dos_protos.h>
Returns the last AmigaDOS error message.
Synopsis: error_code = IoErr();
error_code: (LONG) The function will return one of the below
listed error flags: (The flags are defined in
header file "dos/dos.h")
ERROR_NO_FREE_STORE:
Out of memory!
ERROR_TASK_TABLE_FULL:
To many processes running!
ERROR_BAD_TEMPLATE:
Incorrect command line template!
ERROR_BAD_NUMBER:
Incorrect number/value!
ERROR_REQUIRED_ARG_MISSING:
Required arguemnt was not set!
ERROR_KEY_NEEDS_ARG:
The argument after the keyword is missing!
ERROR_TOO_MANY_ARGS:
Too many arguments on the command line!
ERROR_UNMATCHED_QUOTES:
Incorrect number of quotations!
ERROR_LINE_TOO_LONG:
Too long command line!
ERROR_FILE_NOT_OBJECT:
The file can not be found and/or executed
ERROR_INVALID_RESIDENT_LIBRARY:
The AmigaDOS library is too old!
ERROR_NO_DEFAULT_DIR:
There does not exist a default directory!
ERROR_OBJECT_IN_USE:
The object is currently used and is locked!
ERROR_OBJECT_EXISTS:
The object already exist and can not be deleted!
ERROR_DIR_NOT_FOUND:
Unknown directory!
ERROR_OBJECT_NOT_FOUND:
Unknown file or device!
ERROR_BAD_STREAM_NAME:
The string contained invalid commands/parameters!
ERROR_OBJECT_TOO_LARGE:
The object is too big!
ERROR_ACTION_NOT_KNOWN:
Impossible command used on a device!
ERROR_INVALID_COMPONENT_NAME:
Too long name or invalid signs used!
ERROR_INVALID_LOCK:
Invalid type of lock used!
ERROR_OBJECT_WRONG_TYPE:
Incorrect object type used!
ERROR_DISK_NOT_VALIDATED:
The disk has not yet been validated or is damaged!
ERROR_DISK_WRITE_PROTECTED:
The disk is write protected!
ERROR_RENAME_ACROSS_DEVICES:
Can not rename an object to another device!
ERROR_DIRECTORY_NOT_EMPTY:
Can not delete the directory since it is not empty!
ERROR_TOO_MANY_LEVELS:
Too many soft links used!
ERROR_DEVICE_NOT_MOUNTED:
Device not available!
ERROR_SEEK_ERROR:
Tried to move the file cursor outside the file!
ERROR_COMMENT_TOO_BIG:
Too long comment used!
ERROR_DISK_FULL:
The disk is full!
ERROR_DELETE_PROTECTED:
The object is protected from being deleted!
ERROR_WRITE_PROTECTED:
The object is protected from being overwritten
ERROR_READ_PROTECTED:
The object is protected from being read!
ERROR_NOT_A_DOS_DISK:
The disk is not of AmigaDOS format!
ERROR_NO_DISK:
No disk in the drive!
ERROR_NO_MORE_ENTRIES:
No more objects in the directory!
ERROR_IS_SOFT_LINK:
Can not do it on a soft link!
ERROR_OBJECT_LINKED:
The object is linked!
ERROR_BAD_HUNK:
Incorect hunk found!
ERROR_NOT_IMPLEMENTED:
The feature has not been implemented yet!
ERROR_RECORD_NOT_LOCKED:
The record was not locked!
ERROR_LOCK_COLLISION:
Two locks collided!
ERROR_LOCK_TIMEOUT:
The time to get the lock has expired!
ERROR_UNLOCK_ERROR:
Problems with unlocking the object!
ERROR_BUFFER_OVERFLOW:
Some internal buffer was too small!
ERROR_BREAK:
The executin was stopped!
ERROR_NOT_EXECUTABLE:
The file is protected from being executed!
Please note that all dos function does not use the global error
code! You should therefore, of course, only use it when you
have been told that it will work.
Here is a simple example on how to use the IoErr() function:
/* Store the error code here: */
LONG error_code;
- - -
/* Collect the global error code: */
error_code = IoErr();
/* Print the error code: */
printf( "Error code: %d - ", error_code );
/* Examine the error code: */
switch( error_code )
{
case ERROR_NO_FREE_STORE: printf( "Out of memory!\n" ); break;
case ERROR_TASK_TABLE_FULL: printf( "To many processes running!\n" );
break;
/* and so on... */
default:
printf( "Unknown error reported!\n" );
}
See also: SetIoErr()
---------------------------------------------------------------
Some dos functions, FRead() and FWrite(), were badly written
and do not "clear" the error code before they are executed. It
is therefore impossible to know if the value in the global
error variable is valid after you have executed these
functions - it might be an old value in the global error
variable.
To solve this problem you must therefore "clear" the global
error variable yourself (set the variable to 0) before you
execute these badly written functions.
To alter the global error variable simply call the "SetIoErr()"
function.
---------------------------------------------------------------
SetIoErr()
ROM library: "dos.library/SetIoErr", (V36+)
#include <clib/dos_protos.h>
Alters the global dos error variable.
Synopsis: old_value = SetIoErr( new_value );
old_value: (LONG) The function will return the "old" error
value. See "IoErr()" for more information about
the possible error codes.
new_value: (LONG) The new value you want to store in the
global error variable. When you want to "clear"
the variable you should set this field to 0.
Here is a simple example on how to use the SetIoErr() function:
/* Store the "old" error code here: */
LONG old_error_code;
- - -
/* Set the global error variable to 0: */
/* ("Clear" the global error variable) */
old_error_code = SetIoErr( 0 );
/* Print the "old" error code: */
printf( "Old error code: %d\n", old_error_code );
See also: IoErr()
---------------------------------------------------------------
Although your own program also can use this global error
variable with the help of SetIoErr() and IoErr(), it is not
recommended. It is better to use your own variables if needed.
8.3 HOW TO EXECUTE "SHELL" COMMANDS FROM A C PROGRAM
You can execute "Shell" commands from C with the help of the
"Execute()" function. Indeed, you can actually execute any
type of programs (not only the standard commands like "dir",
"list", "type" etc...) which you can access from the "Shell".
---------------------------------------------------------------
Execute()
ROM library: "dos.library/Execute", (Any Version)
#include <clib/dos_protos.h>
Executes "Shell" commands from C.
Synopsis: ok = Execute( com_str, def_input, def_output );
ok: (LONG) If the command could successfully be
launched TRUE will be returned, else FALSE is
returned.
Please note that the function will not wait for
the executed program to be completed, and the
function can consequently not know if the
executed program finished its task successfully
or failed. The returned value indicates only if
the program was successfully started (launched)
or not.
com_str: (STRPTR) Pointer to a string which contains the
command line you want to have executed. Here are
some examples:
"Dir"
"C:Dir RAM: All"
"C:Copy DH0:Car.doc to DF1:"
"MyPaintProg Flowers.pic"
def_input: (BPTR) Pointer to the "default input handler".
This filed is usually set to NULL, since a
default input handler is usually not needed.
See chapter 6 "Handlers" for more information
about handlers.
def_output: (BPTR) Pointer to the "default output handler".
See chapter 6 "Handlers" for more information
about handlers.
If the program you executed do not use the
default output handler you can set this field
to NULL.
Note! The "run" program must be available in the "C:" directory
if you use Execute() with dos library V34 or earlier.
Here is a simple example on how to use the Execute() function:
/* Store the result code here: */
BOOL ok;
/* A "BCPL" pointer to our "default output */
/* handler": (a Console Window) */
BPTR my_console;
- - -
/* Open a Console Window which will be used as our */
/* default output handler: */
my_console =
Open( "CON:0/0/640/100/Test/CLOSE/WAIT", MODE_NEWFILE );
/* Have we successfully opened the Console Window? */
if( !my_console )
exit( 20 ); /* Exit with an error code: */
/* Execute the "Shell" command "Dir". We do not use */
/* and "input" handler, and the "output" should be */
/* sent to our Console Window. */
ok = Execute( "C:Dir RAM:", NULL, my_console );
/* OK? */
if( ok )
printf( "The \"Shell\" command was successfully executed!\n" );
else
printf( "Error! Could not execute the \"Shell\" command!\n" );
- - -
/* Close the Console Window: */
Close( my_console );
See also: Open(), Close()
---------------------------------------------------------------